package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.domain.Barrio;



public class JDBCBarrioDAO implements IBarrioDAO {

	ICentroCulturalDAO centro;

	IEventoDAO evento;

	IUsuarioDAO usuario;

	public JDBCBarrioDAO() {

		centro = new JDBCCentroCulturalDAO();
		evento = new JDBCEventoDAO();
		usuario = new JDBCUsuarioDAO();
	}

	public List selectCalles(Connection conn, Integer barrioOID) {

		PreparedStatement stmt = null;
		List searchResults = new LinkedList();
		ResultSet result = null;
		String nombre = null;

		try {

			String sql = "SELECT nombre FROM Calles C WHERE C.barrioOID ='"
					+ barrioOID + "'";
			stmt = conn.prepareStatement(sql);

			result = stmt.executeQuery();

			while (result.next()) {
				searchResults.add(result.getString("nombre"));
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {

			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;

	}
	
	public List selectBarriosMunicipio(Connection conn, Integer municipioOID) {

		String sql = "SELECT * FROM Barrio WHERE (municipioOID LIKE '" + municipioOID
				+ "')";

		PreparedStatement stmt = null;
		ResultSet result = null;
		List barrios = new LinkedList();

		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			while (result.next()) {
				List c = new LinkedList();
				Barrio b = new Barrio();
				b.setNombre(result.getString("nombre"));
				Integer zoid = result.getInt("barrioOID");
				c.addAll(selectCalles(conn, zoid));
				b.setCalles(c);
				b.setCentrosCulturales(centro.selectCentroCulturalBarrio(conn, zoid));
				b.setEventos(evento.selectEventoBarrio(conn, zoid));
				b.setUsuarios(usuario.selectAllUsuariosBarrio(conn, zoid));
				barrios.add(b);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}

				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}

		return barrios;
	}

	
	
}
